package g4p.newg4p;

import g4p_controls.G4P;
import g4p_controls.GAlign;
import g4p_controls.GButton;
import g4p_controls.GCheckbox;
import g4p_controls.GControlMode;
import g4p_controls.GEvent;
import g4p_controls.GKnob;
import g4p_controls.GLabel;
import g4p_controls.GOption;
import g4p_controls.GSlider;
import g4p_controls.GStick;
import g4p_controls.GToggleControl;
import g4p_controls.GToggleGroup;
import g4p_controls.GValueControl;
import processing.core.PApplet;


public class StickTest extends PApplet {

	GStick s0;
	
	public void setup(){
		size(500,280);
		
		s0 = new GStick(this, 100, 40, 90, 90);

		makeStickConfigControls();
	}
	
	public void draw(){
		background(bgcol);
		fill(227,230,255);
		noStroke();
		rect(width - 140, 0, 140, height);
		rect(0, height - 90 , width - 140, 90);
	}
	
	public void handleStickEvents(GStick stick, GEvent event) { 
		lblValueState.setText("" + s0.getPosition());
		lblValueX.setText("" + s0.getStickX());
		lblValueY.setText("" + s0.getStickY());
	}
	
	// =================================================================================
	// ===================            New pde Tab             ==========================

	public void handleSliderEvents(GValueControl slider, GEvent event) { 
		if(slider == sdrBack)
			bgcol = slider.getValueI();	
	}

	public void handleKnobEvents(GValueControl knob, GEvent event) { 
		if(knbAngle == knob)
			s0.setRotation(knbAngle.getValueF(), GControlMode.CENTER);
	}

	public void handleButtonEvents(GButton button, GEvent event) { 
		if(button.tagNo >= 1000)
			s0.setLocalColorScheme(button.tagNo - 1000);
	}
	

	public void handleToggleControlEvents(GToggleControl option, GEvent event) {
		if(option == cbxOpaque)
			s0.setOpaque(option.isSelected());
		else if(option == opt4)
			s0.setMode(G4P.X4);
		else if(option == opt8)
			s0.setMode(G4P.X8);
	}

	GToggleGroup modeGroup = new GToggleGroup();
	GOption opt4, opt8;
	GCheckbox cbxOpaque;
	GSlider sdrBack;
	GLabel lblState, lblX, lblY;
	GLabel lblValueState, lblValueX, lblValueY;
	GKnob knbAngle;
	GButton[] btnColours = new GButton[8];

	int bgcol;
	
	public void makeStickConfigControls(){
		// Create colour scheme selectors
		int x = width - 42, y = 2;
		for(int i = 0; i < btnColours.length; i++){
			btnColours[i] = new GButton(this, x, y + i * 20, 40, 18, "" + (i+1));
			btnColours[i].tag = "Button: " + (i+1);
			btnColours[i].setLocalColorScheme(i);
			btnColours[i].tagNo = 1000+i;
		}	
		// Create sliders
		x = width-120; y = 162;
		bgcol = 200; 
		sdrBack = new GSlider(this, x, y, 162, 80, 12);
		sdrBack.setLimits(bgcol, 0, 255);
		sdrBack.setRotation(-PI/2);
		sdrBack.setTextOrientation(G4P.ORIENT_RIGHT);
		sdrBack.setEasing(20);
		sdrBack.setShowValue(true);
		sdrBack.setShowTicks(true);

		x = width - 100; y = 200;
		knbAngle = new GKnob(this, x, y, 70, 70, 0.6f);
		knbAngle.setTurnRange(0, 360);
		knbAngle.setLimits(0.0f, 0.0f, TWO_PI);
		knbAngle.setTurnMode(G4P.CTRL_ANGULAR);
		knbAngle.setIncludeOverBezel(true);
		knbAngle.setNbrTicks(13);
		knbAngle.setStickToTicks(true);

		x = width - 90; y = 170;
		cbxOpaque = new GCheckbox(this, x, y , 70, 18, "Opaque");	
		
		x = 10; y = height - 75;
		lblState = new GLabel(this, x + 34, y, 50, 20, "State");
		lblState.setTextAlign(GAlign.LEFT, null);
		lblValueState = new GLabel(this, x, y, 30, 20, "-1");
		lblValueState.setLocalColorScheme(G4P.GREEN_SCHEME);
		lblValueState.setTextAlign(GAlign.RIGHT, null);
		lblValueState.setOpaque(true);
		
		x = 10; y = height - 50;
		lblX = new GLabel(this, x + 34, y, 50, 20, "X");
		lblX.setTextAlign(GAlign.LEFT, null);
		lblValueX = new GLabel(this, x, y, 30, 20, "0");
		lblValueX.setLocalColorScheme(G4P.GREEN_SCHEME);
		lblValueX.setTextAlign(GAlign.RIGHT, null);
		lblValueX.setOpaque(true);
		
		x = 10; y = height - 25;		
		lblY = new GLabel(this, x + 34, y, 50, 20, "Y");
		lblY.setTextAlign(GAlign.LEFT, null);
		lblValueY = new GLabel(this, x, y, 30, 20, "0");
		lblValueY.setLocalColorScheme(G4P.GREEN_SCHEME);
		lblValueY.setTextAlign(GAlign.RIGHT, null);
		lblValueY.setOpaque(true);
		
		x = 220; y = height - 50;
		modeGroup = new GToggleGroup();
		opt4 = new GOption(this, x, y, 100, 20, "4-way");
		opt8 = new GOption(this, x, y+25, 100, 20, "8-way");
		modeGroup.addControls(opt4, opt8);
		opt4.setSelected(true);
	}
}
